import { Service } from '../../services/service';
export type VerifyCodeType = 'reigster' | 'changeMobile';
import * as chitu from "maishu-chitu";
import { PermissionService } from 'maishu-services-sdk';
import md5 = require("js-md5");
import { config } from '../config';

export let userInfo = new chitu.ValueStore<UserInfo>();
export class MemberService extends Service {

    constructor() {
        super();
    }

    private url(path: string) {
        return `${config.memberUrl}${path}`;
    }

    userInfo(): Promise<UserInfo> {
        let url = this.url(`User/CurrentUserInfo`);//this.url('User/CurrentUserInfo');
        return this.get<UserInfo>(url);
    }

    saveUserInfo(userInfo): Promise<any> {
        let url = this.url('/Member/SaveUserInfo');
        return this.putByJson(url, userInfo);
    }

    sentVerifyCode(mobile: string, type: VerifyCodeType): Promise<{ smsId: string }> {
        console.assert(mobile != null);
        let url = `sms/sendVerifyCode`;
        return this.putByJson(url, { mobile, type });
    }

    checkVerifyCode(smsId: string, verifyCode: string) {
        let url = `sms/checkVerifyCode`;
        return this.getByJson(url, { smsId, verifyCode });
    }


    /** 发送验证码到指定的手机 */
    sentRegisterVerifyCode(mobile: string): Promise<{ SmsId: string }> {
        console.assert(mobile != null);
        // let url = `UserMember/SMS/SendRegisterVerifyCode`;
        let url = 'user'
        return this.postByJson(url, { mobile, type: 'register' });
        // return this.sentVerifyCode(mobile, 'reigster');
    }

    /** 用户注册 */
    async register(mobile: string, password: string, smsId: string, verifyCode: string) {
        password = md5(password);
        let ps = this.createService(PermissionService)
        let r = await ps.user.register(mobile, password, smsId, verifyCode)
        Service.token.value = r.token;
    }

    async login(username: string, password: string): Promise<LoginResult> {
        let ps = this.createService(PermissionService);
        password = md5(password);
        let r = await ps.user.login(username, password);
        Service.token.value = r.token;
        return r;
    }

    resetPassword(mobile: string, password: string, smsId: string, verifyCode: string) {
        let url = `user/resetPassword`;
        return this.putByJson(url, { mobile, password, smsId, verifyCode }).then(data => {
            return data;
        });
    }

    changePassword(password: string, smsId: string, verifyCode: string) {
        let url = `user/changePassword`;
        return this.putByJson(url, { password, smsId, verifyCode }).then(data => {
            return data;
        });
    }

    changeMobile(mobile: string, smsId: string, verifyCode: string) {
        let url = `user/changeMobile`;
        return this.putByJson(url, { mobile, smsId, verifyCode });
    }

    private _store: Store;
    async store() {
        if (this._store) {
            return this._store;
        }
        let url = this.url("Application/Item")
        let store = await this.getByJson<Store>(url, { id: this.applicationId });
        if (store == null)
            return null;

        this._store = store;
        return this._store;
    }
}

